package com.probiz.estoresa.system.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.probiz.estore.common.model.system.CompanyInfo;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.system.service.CompanyInfoManager;


public class CompanyInfoController extends GenericStoreAdminController<CompanyInfo> {
    private CompanyInfoManager companyInfoManager = null;
    public void setCompanyInfoManager(CompanyInfoManager companyInfoManager) {
        this.companyInfoManager = companyInfoManager;
    }
   
    public ModelAndView defaultAction(HttpServletRequest request,
                                      HttpServletResponse response){
		try {
			return super.edit(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView("error");
		}
    }    

    public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("entering 'onSubmit' method...");
		}

		CompanyInfo companyInfo = formBackingObject(request);
		
		ServletRequestDataBinder binder = bindAndValidate(request, companyInfo);
		BindException errors = new BindException(binder.getBindingResult());

		boolean isNew = (companyInfo.getCompanyId() == null);
		if (!errors.hasErrors()) {
			companyInfoManager.saveCompanyInfo(companyInfo);
			String key = (isNew) ? "companyInfo.added" : "companyInfo.updated";
			String message = getMessage(key, new Object[] { companyInfo
					.getCompanyName() });
			saveMessage(request, message);
		}

		return new ModelAndView(new RedirectView("companyInfo.html?doAction=edit"));
	}

	protected CompanyInfo formBackingObject(HttpServletRequest request){
		String companyId = request.getParameter("companyId");
		CompanyInfo companyInfo = null;

		if (!"".equals(request.getParameter("version"))
				&& !StringUtils.isEmpty(companyId)) {
			try {
				Integer pkey = new Integer(companyId);
				companyInfo = companyInfoManager.getById(pkey);
			} catch (Exception ex) {
				// Error retrieving value - so assume it is new
				logger.error(ex);
				companyInfo = new CompanyInfo();
			}
		} else {
			companyInfo = this.companyInfoManager.getDefaultCompany();
		}

		// for unit tests - so we can grab an object to save
		// request.setAttribute(Constants.CompanyInfo_EDIT_KEY, companyInfo);
		return companyInfo;
	}
    
	@Override
	protected String getEntityName(CompanyInfo entity) {
		return entity.getCompanyName();
	}


	@Override
	protected void initController() throws Exception {
		this.mgr = companyInfoManager;
	}

	@Override
	protected void preSave(HttpServletRequest request, CompanyInfo entity, BindException errors) {
		
	}
}
